//
//  ShowPhotoViewController.m
//  iEPD
//
//  Created by ENVANG on 1/6/13.
//  Copyright (c) 2013 Mobioneer Ltd. All rights reserved.
//

#import "ShowPhotoViewController.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface ShowPhotoViewController () <UINavigationControllerDelegate,MFMailComposeViewControllerDelegate,UIImagePickerControllerDelegate>
{
    NSInteger _typeAction;
}
@end

@implementation ShowPhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.title = self.titleShowPhoto;
    NSLog(@"title = %@",self.title);
    self.imgView.image = [UIImage imageNamed:@"14.jpg"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionSheet)];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.hidesBottomBarWhenPushed = YES;
   
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}
- (IBAction)onTap
{
    [[self navigationController] setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:YES];
    //self.imgView.contentMode = UIViewContentModeScaleAspectFill;
}
- (IBAction)twoTap:(UIGestureRecognizer *) gestureRecognizer
{
    float newScale;
    if (self.scrollView.zoomScale < self.scrollView.maximumZoomScale) {
        newScale = self.scrollView.maximumZoomScale;
    }
    else{
        newScale = self.scrollView.minimumZoomScale;
    }
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
     [self.scrollView zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the _imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [self.scrollView frame].size.height / scale;
    zoomRect.size.width  = [self.scrollView frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imgView;
}
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//    //Get all the touches.
//    NSSet *allTouches = [event allTouches];
//    
//    //Number of touches on the screen
//    switch ([allTouches count])
//    {
//        case 1:
//        {
//            //Get the first touch.
//            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
//            
//            switch([touch tapCount])
//            {
//                case 1://Single tap
//                    
//                    break;
//                case 2://Double tap.
//                    [[self navigationController] setNavigationBarHidden:NO animated:YES];
//                    self.imgView.contentMode = UIViewContentModeScaleAspectFill;
//                    break;
//            }
//        }
//            break;
//    }
//    
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma - UIActionSheetDelegate

- (void)actionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"SAVE_PHOTO", nil),NSLocalizedString(@"SHARE", nil),NSLocalizedString(@"PRINT", nil), nil];
    _typeAction = 1;
    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(_typeAction == 1){
    switch (buttonIndex) {
        case 0:
            NSLog(@"Unsupport");
            break;
        case 1:
        {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"FACEBOOK", nil),NSLocalizedString(@"TWITTER", nil),NSLocalizedString(@"MAIL", nil), nil];
            [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
            _typeAction = 2;
        }
            break;
        case 2:
        {
            NSLog(@"Printed");
        }
            break;
        default:
            break;
    }
    } else {
        switch (buttonIndex) {
            case 1:
            {
                TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
                
                //Customize the tweet sheet here
                //Add a tweet message
                [tweetSheet setInitialText:@""];
                [tweetSheet addImage:[UIImage imageNamed:@"envi.png"]];
                //Set a blocking handler for the tweet sheet
                tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
                    [self dismissModalViewControllerAnimated:YES];
                };
                
                //Show the tweet sheet!
                [self presentModalViewController:tweetSheet animated:YES];
            }
                break;
            case 2:
            {
                if ([MFMailComposeViewController canSendMail])
                {
                    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
                    mailer.mailComposeDelegate = self;
                    [mailer setSubject:@""];
                    
                    NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
                    [mailer setToRecipients:toRecipients];
                    UIImage *myImage = [UIImage imageNamed:@"envi.png"];
                    NSData *imageData = UIImagePNGRepresentation(myImage);
                    [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:self.title];
                    NSString *emailBody = @"";
                    [mailer setMessageBody:emailBody isHTML:YES];
                    [self presentModalViewController:mailer animated:YES];
                    
                }
                else
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR", nil)
                                                                    message:NSLocalizedString(@"NO_MAIL", nil)
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles: nil];
                    [alert show];
                }
            }
                break;
            default:
                break;
        }
    }
}

#pragma mark - MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - UIImagePickerControllerDelegate
@end
